load("@fmeum_rules_jni//jni:defs.bzl", "cc_jni_library")
load("//bazel:compat.bzl", "MULTI_PLATFORM", "SKIP_ON_WINDOWS")

DYNAMIC_SYMBOLS_TO_EXPORT = [
    "__sancov_lowest_stack",
    "__sanitizer_cov_8bit_counters_init",
    "__sanitizer_cov_pcs_init",
    "__sanitizer_cov_trace_cmp1",
    "__sanitizer_cov_trace_cmp4",
    "__sanitizer_cov_trace_cmp4",
    "__sanitizer_cov_trace_cmp8",
    "__sanitizer_cov_trace_const_cmp1",
    "__sanitizer_cov_trace_const_cmp4",
    "__sanitizer_cov_trace_const_cmp4",
    "__sanitizer_cov_trace_const_cmp8",
    "__sanitizer_cov_trace_div4",
    "__sanitizer_cov_trace_div8",
    "__sanitizer_cov_trace_gep",
    "__sanitizer_cov_trace_pc_indir",
    "__sanitizer_cov_trace_switch",
    "__sanitizer_weak_hook_memcmp",
    "__sanitizer_weak_hook_memmem",
    "__sanitizer_weak_hook_strcasecmp",
    "__sanitizer_weak_hook_strcasestr",
    "__sanitizer_weak_hook_strcmp",
    "__sanitizer_weak_hook_strncasecmp",
    "__sanitizer_weak_hook_strncmp",
    "__sanitizer_weak_hook_strstr",
    "bcmp",
    "jazzer_preload_init",
    "memcmp",
    "memmem",
    "strcasecmp",
    "strcasestr",
    "strcmp",
    "strncasecmp",
    "strncmp",
    "strstr",
]

cc_jni_library(
    name = "jazzer_preload",
    srcs = ["jazzer_preload.c"],
    linkopts = select({
        "@platforms//os:linux": [
            "-Wl,--export-dynamic-symbol=" + symbol
            for symbol in DYNAMIC_SYMBOLS_TO_EXPORT
        ] + [
            "-ldl",
        ],
        "@platforms//os:macos": [
            "-ldl",
        ],
        "//conditions:default": [],
    }),
    platforms = MULTI_PLATFORM,
    target_compatible_with = SKIP_ON_WINDOWS,
    visibility = ["//src/main/java/com/code_intelligence/jazzer:__pkg__"],
    deps = ["//src/main/native/com/code_intelligence/jazzer/driver:sanitizer_hooks_with_pc"],
)
